﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FineSoft.Models
{
	public class Communication
	{
		private static System.DateTime? _ZeroTime;
		protected System.DateTime ZeroTime
		{
			get
			{
				if (_ZeroTime == null)
					_ZeroTime = new System.DateTime(1970, 1, 1);
				return _ZeroTime.Value;
			}
		}

		protected long CurrentTime
		{
			get
			{
				return (long)(System.DateTime.Now - this.ZeroTime).TotalMilliseconds;
			}
		}

		private System.Net.WebClient _WebClient;
		protected System.Net.WebClient WebClient
		{
			get
			{
				if (this._WebClient == null)
					this._WebClient = new System.Net.WebClient();
				return this._WebClient;
			}
		}

		static Communication()
		{
			System.Net.ServicePointManager.SecurityProtocol = System.Net.SecurityProtocolType.Ssl3;
		}

		//protected Newtonsoft.Json.Linq.JToken DownloadJsonToken(string url)
		//{

		//    return this.GetJsonToken(this.WebClient.DownloadString(url));
		//}

		//protected Newtonsoft.Json.Linq.JToken GetJsonToken(string json)
		//{
		//    return Newtonsoft.Json.Linq.JToken.Parse("{" + json + "}");
		//}

		private string _UniqueId;
		public string UniqueId
		{
			get
			{
				if (this._UniqueId == null)
				{
				}
				return this._UniqueId;
			}
		}

		public string GetUniqueId()
		{
			string script = this.WebClient.DownloadString("https://login.weixin.qq.com/jslogin?appid=wx782c26e4c19acffb&redirect_uri=https%3A%2F%2Fwx.qq.com%2Fcgi-bin%2Fmmwebwx-bin%2Fwebwxnewloginpage&fun=new&lang=zh_CN&_=" + this.CurrentTime);
			script = script.Substring(script.IndexOf('"') + 1);
			return script.Substring(0, script.IndexOf('"'));
		}

		public string GetLoginImageUrl()
		{
			string id = this.GetUniqueId();
			if (id == null)
				return null;
			return string.Format("https://login.weixin.qq.com/qrcode/{0}?t=webwx", id);
		}
	}
}